package com.treasure.mall.biz.base;

import com.treasure.mall.zbq.common.Jsr310Utils;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/6/18
 */
public class RedisTemplate extends org.springframework.data.redis.core.RedisTemplate<String, Object> {
    public long getLong(String key, String hashKey) {
        Object object = this.opsForHash().get(key, hashKey);
        if (object != null) {
            return Long.parseLong(object.toString());
        }
        return 0L;
    }

    public int getInt(String key, String hashKey) {
        Object object = this.opsForHash().get(key, hashKey);
        if (object != null) {
            return (int) object;
        }
        return 0;
    }

    public int getInt(String key) {
        Object object = this.opsForValue().get(key);
        if (object != null) {
            return (int) object;
        }
        return 0;
    }

    public BigDecimal getBigDecimal(String key, String hashKey, int scale) {
        Object object = this.opsForHash().get(key, hashKey);
        if (object != null) {
            double value = Double.parseDouble(object.toString());
            return BigDecimal.valueOf(value).setScale(scale, BigDecimal.ROUND_HALF_UP);
        }

        return BigDecimal.ZERO;
    }


    public LocalDateTime getLocalDateTime(String key, String hashKey) {
        Object object = this.opsForHash().get(key, hashKey);
        if (object != null) {
            return Jsr310Utils.DateTime.of(object.toString());
        }
        return null;
    }
}
